package com.clgk.wechatmall.manager.action;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.clgk.wechatmall.common.Response;
import com.clgk.wechatmall.common.ThreadLocals;
import com.clgk.wechatmall.common.constants.Constants;
import com.clgk.wechatmall.common.sql.Page;
import com.clgk.wechatmall.common.util.Md5Util;
import com.clgk.wechatmall.manager.entity.ManagerVo;
import com.clgk.wechatmall.manager.service.ManagerService;

@Controller
@RequestMapping("manager")
public class ManagerAction {

	@Resource
	private ManagerService managerService;
	
	/**
	 * 后台用户登录
	 * @param u
	 * @param request
	 * @return
	 */
	@RequestMapping(value = {"adminLogin"})
	@ResponseBody
	public Response<ManagerVo> adminLogin(ManagerVo u,HttpServletRequest request){
		Response<ManagerVo> response = new Response<ManagerVo>();
		u.setPassword(Md5Util.MD5(Md5Util.MD5(u.getPassword())));
		ManagerVo vo = this.managerService.login(u);
		if(vo!=null){
			response.setData(vo);
			request.getSession().setAttribute(Constants.SESSION_USER_KEY, vo);
			ThreadLocals.setBackUser(vo);
		}else{
			response.setRetCode(-1);
			response.setMessage("账号密码不匹配");
		}
		return response;
	}
	
	
	/**
	 * 获取后台用户
	 * @param u
	 * @param page
	 * @return
	 */
	@RequestMapping(value = {"getUser","api/getUser"})
	@ResponseBody
	public Response<List<ManagerVo>> getUser(ManagerVo u,Page page){
		Response<List<ManagerVo>> response = new Response<List<ManagerVo>>();
		List<ManagerVo> userList = this.managerService.getUserList(u,page);
		response.setData(userList);
		response.setPage(page);
		return response;
	}
	
	/**
	 * 添加/修改后台用户
	 * @param u
	 * @param page
	 * @return
	 */
	@RequestMapping(value = {"addOrUpdateUser","api/addOrUpdateUser"})
	@ResponseBody
	public Response<Long> addOrUpdateUser(ManagerVo u){
		Response<Long> response = new Response<Long>();
//		ManagerVo qryVo = new ManagerVo();
//		qryVo.setAccount(u.getAccount());
//		if(StringUtils.isNotEmpty(qryVo.getAccount())){
//			List<ManagerVo> userList = this.managerService.getUserList(qryVo,null);
//			if(userList!=null && userList.size()>0){
//				response.setRetCode(-1);
//				response.setMessage("账号已存在！");
//				return response;
//			}
//		}
		if(u.getId()!=null){
			this.managerService.updateUser(u,response);
		}else{
			u.setParentid(ThreadLocals.getBackUser().getId());
			this.managerService.saveUser(u,response);
		}
		return response;
	}
	
	/**
	 * 删除后台用户
	 * @param ids
	 * @return
	 */
	@RequestMapping(value = {"delUser","api/delUser"})
	@ResponseBody
	public Response<Integer> delUser(String ids){
		Response<Integer> response = new Response<Integer>();
		response.setData(managerService.delUser(ids));
		return response;
	}
	
	
}
